package com.hjx.other;

/**
 * 循环翻转链表
 *
 * @author houjinxin
 * @version 创建时间：2019/11/1.
 */
public class LoopReverseList {

    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            //1、暂存当前节点的下一个节点
            ListNode nextTemp = curr.next;
            //2、当前节点的下一个节点指向上一个节点
            curr.next = prev;
            //3、更新上一个节点为当前节点，以备下次循环使用
            prev = curr;
            //4、将当前节点更新为下一节点
            curr = nextTemp;
        }
        return prev;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = listNode1.next = new ListNode(2);
        ListNode listNode3 = listNode2.next = new ListNode(3);
        listNode3.next = new ListNode(4);
//        ListNode listNode1 = null;

        ListNode temp = reverseList(listNode1);
        while (temp != null){
            System.out.println(temp.val);
            temp = temp.next;
        }
    }
}

